/*
* © 2010 Autodata Solutions Company. All Rights Reserved
* This source code is the confidential and proprietary information of Autodata Solutions Company.
* The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
* without the prior written consent of Autodata Solutions Company.
*/
Ext.ns('net.autodata.compare.plugins');
(function() {
    var EVENTS = net.autodata.EventBus.EVENTS;

    /**
    * This plugin is used to bind the CompetitorToolbar to the events of the EventBus.
    * This will keep the CompetitorToolbar always insync with the display of the CompareVehiclesPanel.
    * @class CompetitorToolbarEventBusConnector
    * @extends Ext.util.Observable
    * @namespace net.autodata.compare.plugins
    */
    var Plugin = Ext.extend(Ext.util.Observable, {
        /**
        * @property inventoryPanel
        * @type net.autodata.compare.InventoryPanel
        */
        toolbar: undefined,

        /**
        * @method init
        * @param {net.autodata.compare.CompetitorToolbar} toolbar
        */
        init: function(toolbar) {
            this.toolbar = toolbar;
            var eb = net.autodata.EventBus.getInstance();
            eb.on(EVENTS.READY, this.loadStore, this);
            eb.on(EVENTS.SESSION_COMPETITOR_CHANGED, this.loadStore, this);
            eb.on(EVENTS.SESSION_COMPETITOR_REMOVED, this.loadStore, this);
        },

        loadStore: function() {
            var store = this.toolbar.getStore();
            store.load();
        },


        /**
        * Used to clean up all event bindings made by this plugin
        * @private
        */
        onDestroy: function() {
            var eb = net.autodata.EventBus.getInstance();
            eb.un(EVENTS.READY, this.loadStore, this);
            eb.un(EVENTS.SESSION_COMPETITOR_CHANGED, this.loadStore, this);
            eb.un(EVENTS.SESSION_COMPETITOR_REMOVED, this.loadStore, this);
        }

    });

    net.autodata.compare.plugins.CompetitorToolbarEventBusConnector = Plugin;

})();